package com.dragon.queue;

import java.lang.reflect.Field;
import java.util.ArrayDeque;

/**
 * 取模测试
 *
 * @author dragon
 * @since 2019/11/19
 */
public class Test {

    private static class Person {
        String name;
        Integer age;

        public Person(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }

    public static void main(String[] args) throws Exception {
        ArrayDeque<Person> deque = new ArrayDeque<>();
        Person dragon = new Person("dragon", 12);
        Person mars = new Person("mars", 17);
        Person hero = new Person("hero", 123);
        deque.offer(dragon);
        deque.offer(mars);
        deque.offer(hero);

        ArrayDeque<Person> cloneDeque = deque.clone();
        System.out.println("deque: " + deque);
        System.out.println("cloneDeque: " + cloneDeque);
        dragon.name = "dragon1992";
        System.out.println("========================");
        System.out.println("deque: " + deque);
        System.out.println("cloneDeque: " + cloneDeque);
        System.out.println("deque == cloneDeque: " + (deque == cloneDeque));

        Class<? extends ArrayDeque> aClass = deque.getClass();
        Class<? extends ArrayDeque> aClass1 = cloneDeque.getClass();

        Field elements = aClass.getDeclaredField("elements");
        elements.setAccessible(true);
        Object[] elementArray = (Object[]) elements.get(deque);


        Field elements1 = aClass1.getDeclaredField("elements");
        elements1.setAccessible(true);
        Object[] cloneElementArray = (Object[]) elements1.get(cloneDeque);

        System.out.println(elementArray[0] == cloneElementArray[0]);

        System.out.println(elementArray == cloneElementArray);
    }

}
